package org.jeecg.modules.zcgl.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.zcgl.entity.ReceivablesInfo;
import org.jeecg.modules.zcgl.enums.PayStatusEnum;
import org.jeecg.modules.zcgl.enums.YesNoEnum;
import org.jeecg.modules.zcgl.service.IReceivablesInfoService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 自动扣费
 */
@Slf4j
public class AutomaticDeductionJob implements Job {


	@Autowired
	private IReceivablesInfoService receivablesInfoService;

	@Override
	public void execute(JobExecutionContext jobExecutionContext) {
		long start = System.currentTimeMillis();
		log.info("自动扣费开始：" + start);
        LambdaQueryWrapper<ReceivablesInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReceivablesInfo :: getPayStatus, PayStatusEnum.UNPAID.getStatus());
        queryWrapper.eq(ReceivablesInfo :: getAmountConfirmation, YesNoEnum.YES.getStatus());
        queryWrapper.eq(ReceivablesInfo :: getIzOwe, YesNoEnum.NO.getStatus());
		queryWrapper.apply("DATE(pay_start_date) <= DATE('" + DateUtils.formatDate(DateUtils.DATE_FORMAT) + "')");
		String a=DateUtils.formatDate(DateUtils.DATE_FORMAT);

		List<ReceivablesInfo> receivablesInfoList = receivablesInfoService.list(queryWrapper);
        if (receivablesInfoList != null && receivablesInfoList.size() > 0){
			receivablesInfoList.stream().forEach(receivablesInfo -> {
				try {
					receivablesInfoService.automaticDeduction(receivablesInfo);
				} catch (Exception e) {
					e.printStackTrace();
					log.info("自动扣费错误：" + e.getMessage());
				}
			});
			log.info("共计：" + receivablesInfoList.size() + "条数据。");
		}
		long end = System.currentTimeMillis();
		log.info("自动扣费结束：" + end);
		log.info("自动扣费耗时：" + (end-start)/1000 + "S");
	}
}
